const app = getApp();
import { hint } from "../../../utils/util";
import Apipublic from "../../../models/api";
import { Houses } from "../../../models/houses";
const house = new Houses();
const api = new Apipublic();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: getApp().globalImgUrl.config.baseImgUrl,
    current: 1,
    isSuccess: false,
    commId: "",
    commName: "",
    form: {},
    communityShow: false,
    build: false,
    unit: false,
    floor: false,
    room: false,
    communityList: [],
    buildList: [],
    unitList: [],
    floorList: [],
    roomList: [],
    //---------------------------------
    houseCount: "",
    area: "",
    rentCount: "",
    rentArea: "",
    houseCard: "",
    categoryShow: false,
    natureShow: false,
    useShow: false,
    situationShow: false,
    structureShow: false,
    numberShow: false,
    rentShow: false,
    rentNumShow: false,
    rentUseShow: false,
    modelShow: false,
    categoryList: [],
    natureList: [],
    useList: [],
    situationList: [],
    structureList: [],
    isRentList: [],
    rentUseList: [],
    modelList: [],
  },
  onShow() {
    this.getItem();
  },
  handlerGobackClick() {
    wx.navigateBack();
  },
  //打开弹出框
  handleShowPicker(e) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      [name]: true,
    });
  },
  //关闭/取消弹出框
  closePopup(e) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      [name]: false,
    });
  },
  blurInput(e) {
    let value = e.detail.value;
    if (value) {
      let data = {
        commName: value,
      };
      api.userInfoAddress(data).then((res) => {
        if (res.result instanceof Array && res.result.length) {
          this.setData({
            communityShow: true,
            communityList: res.result,
          });
        } else {
          hint("请输入正确的小区名称");
          this.setData({
            commId: "",
            commName: "",
            communityList: [],
          });
        }
      });
    }
  },
  selectRoom(e) {
    let type = e.currentTarget.dataset.name;
    let data = {};
    let commId = this.data.commId;
    let buildId = this.data.form.buildId;
    let unitId = this.data.form.unitId;
    let floorId = this.data.form.floorId;
    //选楼
    if (type == "build") {
      if (commId) {
        data = {
          commId,
        };
      } else {
        hint("请选择小区");
        return;
      }
    }
    //选单元
    if (type == "unit") {
      if (buildId) {
        data = {
          buildId,
        };
      } else {
        hint("请选择楼号");
        return;
      }
    }
    //选楼层
    if (type == "floor") {
      if (unitId) {
        data = {
          unitId,
        };
      } else {
        hint("请选择单元");
        return;
      }
    }
    //选房间
    if (type == "room") {
      if (floorId) {
        data = {
          floorId,
        };
      } else {
        hint("请选择楼层");
        return;
      }
    }
    api.userInfoAddress(data).then((res) => {
      this.setData({
        [type]: true,
        [type + "List"]: res.result || [],
      });
    });
  },
  //小区
  showConfirm1(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let commId = this.data.commId;
      if (value.commId != commId) {
        let form = this.data.form;
        form.buildId = "";
        form.buildName = "";
        form.unitId = "";
        form.unitName = "";
        form.floorId = "";
        form.floorName = "";
        form.houseId = "";
        form.houseName = "";
        this.setData({
          form,
        });
      }
      this.setData({
        commId: value.commId,
        commName: value.commName,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  //楼号
  showConfirm2(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      let buildId = form.buildId;
      if (value.buildId != buildId) {
        form.unitId = "";
        form.unitName = "";
        form.floorId = "";
        form.floorName = "";
        form.houseId = "";
        form.houseName = "";
        this.setData({
          form,
        });
      }
      form = { ...form, buildId: value.buildId, buildName: value.buildName };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  //单元
  showConfirm3(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      let unitId = form.unitId;
      if (value.unitId != unitId) {
        form.floorId = "";
        form.floorName = "";
        form.houseId = "";
        form.houseName = "";
        this.setData({
          form,
        });
      }
      form = { ...form, unitId: value.unitId, unitName: value.unitName };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  //楼层
  showConfirm4(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      let floorId = form.unitId;
      if (value.floorId != floorId) {
        form.houseId = "";
        form.houseName = "";
        this.setData({
          form,
        });
      }
      form = { ...form, floorId: value.floorId, floorName: value.floorName };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  //房间
  showConfirm5(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = { ...form, houseId: value.houseId, houseName: value.houseName };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  //下一步
  nextStep() {
    let commId = this.data.commId;
    let commName = this.data.commName;
    let form = this.data.form;
    if (!commName) {
      hint("请选择小区");
      return;
    }
    if (!form.buildName) {
      hint("请选择楼号");
      return;
    }
    if (!form.unitName) {
      hint("请选择单元");
      return;
    }
    if (!form.floorName) {
      hint("请选择楼层");
      return;
    }
    if (!form.houseName) {
      hint("请选择房间");
      return;
    }
    form = { ...form, commId, commName };
    let memberId = wx.getStorageSync("user").memberId;
    house.homeowner({ memberId, houseId: form.houseId }).then((res) => {
      if (res.code == 1001) {
        hint(res.message);
        return;
      }
      if (res.code == 1000) {
        this.setData({
          form,
          current: 2,
          isSuccess: true,
        });
      }
    });
  },
  getItem() {
    //房屋类别 house_category
    api.dictItemList("house_category").then((res) => {
      this.setData({
        categoryList: res.result || [],
      });
    });
    // 房屋性质 house_nature
    api.dictItemList("house_nature").then((res) => {
      this.setData({
        natureList: res.result || [],
      });
    });
    // 房屋用途	house_use
    api.dictItemList("house_use").then((res) => {
      this.setData({
        useList: res.result || [],
      });
    });
    // 房屋状态	house_status
    api.dictItemList("house_status").then((res) => {
      this.setData({
        situationList: res.result || [],
      });
    });
    // 房屋结构	house_struct
    api.dictItemList("house_struct").then((res) => {
      this.setData({
        structureList: res.result || [],
      });
    });
    // 是否出租房   isRent
    api.dictItemList("isRent").then((res) => {
      this.setData({
        isRentList: res.result || [],
      });
    });
    // 出租用途	rent_use
    api.dictItemList("rent_use").then((res) => {
      this.setData({
        rentUseList: res.result || [],
      });
    });
    // 房屋户型	house_type
    api.dictItemList("house_type").then((res) => {
      this.setData({
        modelList: res.result || [],
      });
    });
  },
  //----------------------------------------------
  showConfirm6(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        houseCategory: value.value,
        houseCategoryDesc: value.text,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm7(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        houseNature: value.value,
        houseNatureDesc: value.text,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm8(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        houseUse: value.value,
        houseUseDesc: value.text,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm9(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        houseStatus: value.value,
        houseStatusDesc: value.text,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm10(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        houseStruct: value.value,
        houseStructDesc: value.text,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm11(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        isRent: value.value,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm12(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        rentUse: value.value,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm13(e) {
    let name = e.currentTarget.dataset.name;
    let value = e.detail.value;
    if (value) {
      let form = this.data.form;
      form = {
        ...form,
        houseType: value.value,
        houseTypeDesc: value.text,
      };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  // previousStep() {
  //   this.setData({
  //     form,
  //     current: 1,
  //     isSuccess: false,
  //   });
  // },
  save() {
    let form = this.data.form;
    let houseCount = this.data.houseCount;
    let area = this.data.area;
    let rentCount = this.data.rentCount;
    let rentArea = this.data.rentArea;
    let houseCard = this.data.houseCard;

    //正整数
    let pattern1 = /^[1-9]\d*$/;
    //正数
    let pattern2 = /^[1-9]\d*.*\d*$/;
    if (!form.houseCategoryDesc) {
      hint("请选择房屋类别");
      return;
    }
    if (!form.houseNatureDesc) {
      hint("请选择房屋性质");
      return;
    }
    if (!form.houseUseDesc) {
      hint("请选择房屋用途");
      return;
    }
    if (!form.houseStatusDesc) {
      hint("请选择房屋现状");
      return;
    }
    if (!form.houseStructDesc) {
      hint("请选择房屋结构");
      return;
    }
    if (!houseCount || !pattern1.test(houseCount)) {
      hint("请输入正确房屋间数");
      return;
    }
    if (!area || !pattern2.test(area)) {
      hint("请输入正确房屋面积");
      return;
    }
    // if (!form.isRentDesc) {
    //   hint("请选择是否出租房");
    //   return;
    // }
    if (!form.isRent) {
      hint("请选择是否出租房");
      return;
    }
    if (form.isRent == "是") {
      if (
        !rentCount ||
        Number(rentCount) > Number(houseCount) ||
        !pattern1.test(rentCount)
      ) {
        hint("请输入正确出租间数");
        return;
      }
      if (
        !rentArea ||
        Number(rentArea) > Number(area) ||
        !pattern2.test(rentArea)
      ) {
        hint("请输入正确出租面积");
        return;
      }
      // if (!form.rentUseDesc) {
      //   hint("请选择出租用途");
      //   return;
      // }
      if (!form.rentUse) {
        hint("请选择出租用途");
        return;
      }
    } else {
      rentCount = 0;
      rentArea = 0;
      form.rentUse = "";
    }
    if (!form.houseTypeDesc) {
      hint("请选择房屋户型");
      return;
    }
    form = {
      ...form,
      houseCount,
      area,
      rentCount,
      rentArea,
      houseCard,
      kinship: 0,
    };
    house.add(form).then((res) => {
      hint(res.message);
      wx.navigateBack();
    });
  },
});
